package `02-lambda_inside_function`

import kotlin.concurrent.thread

fun main() {
    //官方
    thread{
        println("当前线程是${Thread.currentThread()}")
    }

    //自定义的
    val thread01 = myThread{
        println("自定义myThread函数-当前线程是${Thread.currentThread()}")
    }
    thread01.run()

    //自定义的
     myThread02{
        println("自定义myThread02函数-当前线程是${Thread.currentThread()}")
    }
}

fun myThread(action:()->Unit):Thread{
    //实现Java的Thread类
    val thread = object:Thread(){
        override fun run() {
            super.run()
            action() //在子线程中执行lambda
        }
    }
    return thread
}

fun myThread02(start: Boolean = true, action:()->Unit):Thread{
    //实现Java的Thread类
    val thread = object:Thread(){
        override fun run() {
            super.run()
            action() //在子线程中执行lambda
        }
    }
    if(start){
        thread.run()
    }
    return thread
}